﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using HotelManagement.DTO;

namespace HotelManagement.DAO
{
    class AccountDAO:DataProvider
    {
        public AccountDAO()
            :base()
        {

        }

        public bool Insert(AccountDTO account)
        {
            string strInsert = "insert into ACCOUNT(UserName,Password,AccountType,Name,IDNo,Gender,Birth,Address,Description,Picture,PhoneNumber) values (";
            strInsert += "N'" + account.UserName + "',";
            strInsert += "N'" + account.Password + "',";
            strInsert += "N'" + account.AccountType + "',";
            strInsert += "N'" + account.Name + "',";
            strInsert += "N'" + account.IDNo_Passport + "',";
            strInsert += "N'" + account.Gender + "',";
            strInsert += "N'" + account.BirthYear + "',";
            strInsert += "N'" + account.Address + "',";
            strInsert += "N'" + account.Description + "',";
            strInsert += "N'" + account.Picture + "',";
            strInsert += "N'" + account.PhoneNumber + "')";
            return base._ExecuteQuery(strInsert);
        }

        public bool Update(AccountDTO account)
        {
            string strUpdate="update account set ";
            //strUpdate += "UserID='" + account.UserName + "',";
            strUpdate += "Password= N'" + account.Password + "',";
            strUpdate += "AccountType= N'" + account.AccountType + "',";
            strUpdate += "Name= N'" + account.Name + "',";
            strUpdate += "IDNo= N'" + account.IDNo_Passport + "',";
            strUpdate += "Gender= N'" + account.Gender + "',";
            strUpdate += "Birth='" + account.BirthYear + "',";
            strUpdate += "Address= N'" + account.Address + "',";
            strUpdate += "Description= N'" + account.Description + "',";
            strUpdate += "Picture='" + account.Picture + "',";
            strUpdate += "PhoneNumber= N'" + account.PhoneNumber + "' where UserName = '" + account.UserName + "'";
            
            return _ExecuteQuery(strUpdate);
        }

        public bool Delete(AccountDTO account)
        {
            string strDelete = "delete from account where UserName='" + account.UserName + "'";
            
            return _ExecuteQuery(strDelete);
        }

        public DataTable Select()
        {
            string strSelect = "select * from account";
            return base._Select(strSelect);

        }
        public AccountDTO GetInformationOfAUser(string pUserName)
        {
            string strLogin = "select * from account where UserName ='" + pUserName + "'";
           
            SqlCommand cmd = new SqlCommand(strLogin, connection);
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataTable tbl = new DataTable();
            adp.Fill(tbl);
            DataRow row;
            AccountDTO info = new AccountDTO();
            try
            {
                row = tbl.Rows[0];
                info.UserName = row["UserName"].ToString();
                info.Password = row["Password"].ToString();
                info.AccountType = row["AccountType"].ToString();
                info.Name = row["Name"].ToString();
                info.IDNo_Passport = row["IDNo"].ToString();
                info.Gender = row["Gender"].ToString();
                info.BirthYear = DateTime.Parse(row["Birth"].ToString());
                info.Address = row["Address"].ToString();
                info.Description = row["Description"].ToString();
                info.Picture = row["Picture"].ToString();
                info.PhoneNumber = row["PhoneNumber"].ToString();
            }
            catch (System.Exception e)
            {

            }

            return info;
        }
        /*
        public List<string> GetInformationOfAUser(string pUserName)
        {
            string strLogin="select * from account where UserID='"+pUserName+"'";
            if (connection.State == System.Data.ConnectionState.Closed)
            {
                if (!Connect())
                {
                    return null;
                }
            }
            SqlCommand cmd=new SqlCommand(strLogin,connection);
            SqlDataAdapter adp=new SqlDataAdapter(cmd);
            DataTable tbl=new DataTable();
            adp.Fill(tbl);
            DataRow row;
            List<string> info = new List<string>();
            try
            {
                row = tbl.Rows[0];
                info.Add(row["UserID"].ToString());
                info.Add(row["Password"].ToString());
                info.Add(row["AccountType"].ToString());
                info.Add(row["Name"].ToString());
                info.Add(row["IDNo"].ToString());
                info.Add(row["Gender"].ToString());
                info.Add(row["Birth"].ToString());
                info.Add(row["Address"].ToString());
                info.Add(row["Description"].ToString());
                info.Add(row["Picture"].ToString());
            }
            catch (System.Exception e)
            {
                
            }
            
            return info;
        }
         * */
    }
}
